package com.superhelper.common.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.superhelper.common.domain.feie.FResp;
import com.superhelper.common.utils.HttpClientHelper;
import com.superhelper.common.utils.JsonHelper;

public class FeiEService {
    private static final Logger log = LoggerFactory.getLogger(FeiEService.class);
    private final String URL = "http://api.feieyun.cn/Api/Open/";// 不需要修改
    private final String USER = "550604132@qq.com";// *必填*：账号名
    private final String UKEY = "yF7b6qPF6vpZBfVR";// *必填*: 注册账号后生成的UKEY
    private final HttpClientHelper httpClientHelper = new HttpClientHelper();

    @SuppressWarnings("serial")
    private Map<String, String> getSysParams() {
	return new HashMap<String, String>() {
	    {
		String stime = String.valueOf(System.currentTimeMillis() / 1000);
		put("stime", stime);
		put("user", USER.toString());
		put("sig", DigestUtils.shaHex(USER + UKEY + stime));
		put("apiname", "");
	    }
	};
    }

    /**
     * 绑定打印机
     * 
     * @param sn
     * @param code
     * @return 返回打印机编号
     */
    public boolean addprinter(String sn, String code) {
	try {
	    String snList = sn + "#" + code;
	    Map<String, String> params = this.getSysParams();
	    params.put("apiname", "Open_printerAddlist");
	    params.put("printerContent", snList);
	    String result = httpClientHelper.doPost(URL, params);
	    log.info("addprinter->{}", result);
	    if (StringUtils.isNotBlank(result)) {
		FResp resp = JsonHelper.fromJson(result, FResp.class);
		if (resp.getRet() == 0) {
		    return true;
		}
	    }
	} catch (Exception e) {
	    // TODO Auto-generated catch block
	    log.error("addprinter error:{}", e);
	}
	return false;
    }

    /**
     * 打单
     * 
     * @param sn
     * @param order
     * @return 返回打单id
     */
    public String sendToPrint(String sn, String content) {
	try {
	    Map<String, String> params = this.getSysParams();
	    params.put("apiname", "Open_printMsg");
	    params.put("sn", sn);
	    params.put("content", content);
	    String result = httpClientHelper.doPost(URL, params);
	    log.info("sendToPrint->{}", result);
	    if (StringUtils.isNotBlank(result)) {
		FResp resp = JsonHelper.fromJson(result, FResp.class);
		if (resp.getRet() == 0) {
		    return resp.getData().toString();
		}
	    }
	} catch (Exception e) {
	    // TODO Auto-generated catch block
	    log.error("sendToPrint error:{}", e);
	}
	return null;
    }

    /**
     * 查询打印状态
     * 
     * @param orderid
     * @return 已打印返回true,未打印返回false。
     */
    public boolean queryOrderState(String orderid) {
	try {
	    Map<String, String> params = this.getSysParams();
	    params.put("apiname", "Open_queryOrderState");
	    params.put("orderid", orderid);
	    String result = httpClientHelper.doPost(URL, params);
	    log.info("queryOrderState->{}", result);
	    if (StringUtils.isNotBlank(result)) {
		FResp resp = JsonHelper.fromJson(result, FResp.class);
		if (resp.getRet() == 0) {
		    return (Boolean) resp.getData();
		}
	    }
	} catch (Exception e) {
	    // TODO Auto-generated catch block
	    log.error("queryOrderState error:{}", e);
	}
	return false;
    }

    /**
     * 查询打印机状态
     * 
     * @param sn
     * @return 已打印返回true,未打印返回false。
     */
    public boolean queryPrinterState(String sn) {
	try {
	    Map<String, String> params = this.getSysParams();
	    params.put("apiname", "Open_queryPrinterStatus");
	    params.put("sn", sn);
	    String result = httpClientHelper.doPost(URL, params);
	    log.info("queryPrinterState->{}", result);
	    if (StringUtils.isNotBlank(result)) {
		FResp resp = JsonHelper.fromJson(result, FResp.class);
		if (resp.getRet() == 0) {
		    return (Integer) resp.getData() != 1; // 1、离线
							  // 2、在线，工作状态正常
							  // 3、在线，工作状态不正常
		}
	    }
	} catch (Exception e) {
	    // TODO Auto-generated catch block
	    log.error("queryPrinterState error:{}", e);
	}
	return false;
    }

    public static void main(String args[]) {
	FeiEService f = new FeiEService();
	f.addprinter("917504325", "xfjwww3v");
    }
}
